/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.abc;

import echopointng.ExtentEx;
import java.text.SimpleDateFormat;
import java.util.List;
import nextapp.echo2.app.Alignment;
import nextapp.echo2.app.Button;
import nextapp.echo2.app.Color;
import nextapp.echo2.app.Column;
import nextapp.echo2.app.Component;
import nextapp.echo2.app.ContentPane;
import nextapp.echo2.app.Extent;
import nextapp.echo2.app.Font;
import nextapp.echo2.app.Insets;
import nextapp.echo2.app.Label;
import nextapp.echo2.app.Row;
import nextapp.echo2.app.SelectField;
import nextapp.echo2.app.SplitPane;
import nextapp.echo2.app.Table;
import nextapp.echo2.app.TextField;
import nextapp.echo2.app.WindowPane;
import nextapp.echo2.app.event.ActionEvent;
import nextapp.echo2.app.event.ActionListener;
import nextapp.echo2.app.layout.TableLayoutData;
import nextapp.echo2.app.list.DefaultListModel;
import nextapp.echo2.app.table.DefaultTableCellRenderer;
import nextapp.echo2.app.table.DefaultTableModel;
import nextapp.echo2.app.table.TableCellRenderer;
import org.sapra.dao.Empresa;
import org.sapra.dao.Practicante;
import org.sapra.dep.echo.CambioFecha.Aviso;
import org.sapra.echo.Helpers;
import org.sapra.echo.wAviso;
import org.sapra.logon.dLogin;
import org.sapra.main.vPrincipal;

/**
 *
 * @author luiz
 */
public class dBusqueda extends ContentPane{

    private SelectField SelSearch=new SelectField();
    private  TextField txtSearch=new TextField();
    private Table pracSearch=new Table();
    //private CheckBox active=new CheckBox();
    private String title[]={"Id","Nombre Completo","Fecha Inicio","Fecha Termino","Fecha Cancelacion","Beca"};
    private Button editar=new Button();
    private Button cancelar=new Button();
    private vPracticante mVista;
    private Practicante p=new Practicante();
    private List<Practicante> lista=null;
    private vPrincipal princ;
    public dBusqueda(vPracticante m_vista,vPrincipal princ) {
        super();
        if(m_vista != null)mVista = m_vista;
        if(princ != null)this.princ = princ;
        initComponents();
        
    }
    public dBusqueda(vPrincipal princ) {
        super();
        if(princ != null)this.princ = princ;
        initComponents();

    }
    SplitPane sp,sp2;
     private void initComponents(){

                sp=Helpers.SplitPaneHelper(SplitPane.ORIENTATION_HORIZONTAL, new ExtentEx(200,ExtentEx.PX), false);
                sp2=Helpers.SplitPaneHelper(SplitPane.ORIENTATION_HORIZONTAL, new ExtentEx(200,ExtentEx.PX), false);
                sp.add(sp2);

                Column col=new Column();
                Row menu=princ.getMenu();
                Row r1=new Row();
                r1.setAlignment(Alignment.ALIGN_CENTER);
                Row r2=new Row();
                r2.setCellSpacing(new Extent(5));
                r2.setAlignment(Alignment.ALIGN_CENTER);
                acordeon2();
                Button can= Helpers.Button("CANCELAR", r2, new ActionListener(){public void actionPerformed(ActionEvent e) {
                    mVista.ContenedorPrincipal();
                }});
                r1=Helpers.rowTextField("BUSQUEDA:", 2, txtSearch, 2);
                DefaultListModel dlm=new DefaultListModel();
                    dlm.add("Nombre (S)");
                    dlm.add("Apellido Paterno");
                    dlm.add("Apellido Materno");
                    dlm.add("Nombre Completo");
                SelSearch.setModel(dlm);
                //Button bs=new Button("Buscar");
                Button bs= Helpers.Button("BUSCAR", r1, new ActionListener(){public void actionPerformed(ActionEvent e){
                            Empresa emp=dLogin.getEmpresa();
                            String res=txtSearch.getText().trim().toLowerCase();
                            int opc=SelSearch.getSelectedIndex();
                            if(!(opc<0)){
                                if(!res.equals("")){
                                    lista=null;
                                    lista=dPracticante.busqueda(emp.getEmpresaId(),res, opc);
                                    pracSearch.setModel(tableList(lista));
                                }  else{
                                    add(new wAviso("ERROR","Favor de Escribir Algo En la Busqueda"));
                                    return;
                                }
                            }else{
                                add(new wAviso("ERROR","Favor de Seleccionar Una opcion del Combo Busqueda"));
                                return;
                            }

                        }
                    });

                r1.add(new Label("Activo:"));
                r1.add(SelSearch);

                r1.add(bs);
                r1.setAlignment(Alignment.ALIGN_CENTER);
                r1.setCellSpacing(new Extent(10));

                col.add(menu);
                col.add(r1);
                col.setInsets(new Insets(0,15, 0, 0));
                col.add(r2);

                pracSearch =Helpers.Tabla();
                pracSearch.setDefaultHeaderRenderer(new HeaderTable());
                pracSearch.setDefaultRenderer(Object.class,new TableCell());
                pracSearch.setWidth(new Extent(850));
                DefaultTableModel dtm=new DefaultTableModel(6,0);
                for(int i=0;i<title.length;i++){
                        dtm.setColumnName(i,title[i]);
                 }
                pracSearch.setModel(dtm);
                Row lineaTabla=new Row();
                lineaTabla.add(pracSearch);

                col.add(lineaTabla);
                sp.add(col);
                add(sp);

               
     }

     Button mbRenovar2;
     private Button mbEditar2,mvVer2,mbArchivos2,mbBaja2;
     WindowPane ven2;

     private Practicante selecPrac(){
                    int sel = pracSearch.getSelectionModel().getMinSelectedIndex();
                    if(sel < 0){
                         mVista.add(new Aviso("Error de Seleccion", "Favor de Seleccionar un Practicante"));
                         return null; // No hay selecciont
                    }
                    Practicante p = lista.get(sel);


            return p;
     }
     public void acordeon2(){
            Column col_b=new Column();
            mbEditar2=Helpers.Button2("EDITAR",  new ActionListener(){public void actionPerformed(ActionEvent e) {
                                    Practicante p=selecPrac();
                                    if(p!=null)
                                    mVista.MenuEditar(e,2,p);
            }});
            mbRenovar2 = Helpers.Button2("RENOVAR",  new ActionListener(){public void actionPerformed(ActionEvent e) {
                                    Practicante p=selecPrac();
                                    if(p!=null)
                                    mVista.MenuRenovar(e,2,p);
            }});
            mvVer2= Helpers.Button2("DETALLE",  new ActionListener(){public void actionPerformed(ActionEvent e) {
                 int sel = pracSearch.getSelectionModel().getMinSelectedIndex();
                                    Practicante p=selecPrac();
                                    if(p!=null)
                                    mVista.MenuVer(e,2,p);
            }});

            mbBaja2= Helpers.Button2("BAJA",  new ActionListener(){public void actionPerformed(ActionEvent e) {
                                     Practicante p=selecPrac();
                                     if(p!=null)
                                     mVista.MenuBaja(e,2,p);
            }});
            mbArchivos2=Helpers.Button2("DOCUMENTOS",  new ActionListener(){public void actionPerformed(ActionEvent e) {
                                     Practicante p=selecPrac();
                                     if(p!=null)
                                     mVista.MenuArchivos(e,2,p);

            }});

            col_b.add(mbEditar2);
            col_b.add(mbRenovar2);
            col_b.add(mvVer2);
            //col_b.add(mbArchivos2);
            col_b.add(mbBaja2);
            col_b.setCellSpacing(new Extent(5));
            col_b.setInsets(new Insets(new Extent(10)));
       
            col_b.add(mbRenovar2);
            sp2.add(col_b);

     }

     public DefaultTableModel tableList(List<Practicante> lista){
            DefaultTableModel dtm=new DefaultTableModel(6,0);
            SimpleDateFormat spd=new SimpleDateFormat("yyyy-MM-dd");
            for(int i=0;i<title.length;i++){
                dtm.setColumnName(i,title[i]);
            }

            for(int i=0;i<lista.size();i++){
                Object obj[]=new Object[6];
                Practicante p=(Practicante)lista.get(i);
                obj[0]=""+p.getPracticanteId();
                obj[1]=""+p.getNombre()+" "+p.getPaterno()+" "+p.getMaterno();
                obj[2]=""+spd.format(p.getFec_inicioContrato());
                obj[3]=""+spd.format(p.getFec_terminoContrato());
                if(p.getFec_cancelacion()==null)
                    obj[4]="  ";
                else
                    obj[4]=""+spd.format(p.getFec_cancelacion());
                obj[5]=""+p.getSueldo();
                dtm.addRow(obj);
            }
            return dtm;
        }
}

class HeaderTable extends DefaultTableCellRenderer implements TableCellRenderer{

@Override
    public Component getTableCellRendererComponent(Table table,Object value,int column,int row){

        Label label=new Label(value.toString());
        TableLayoutData tld=new TableLayoutData();
        tld.setBackground(new Color(178,34,34));
        label.setForeground(Color.WHITE);
        label.setLayoutData(tld);
        label.setFont(new Font(Font.ARIAL, 1, new Extent(13)));

        return label;
    }
}

class TableCell extends DefaultTableCellRenderer implements TableCellRenderer{

@Override
    public Component getTableCellRendererComponent(Table table,Object value,int column,int row){

        Label label=new Label(value.toString());
        TableLayoutData tld=new TableLayoutData();
        if(row%2==0)
            tld.setBackground(Color.WHITE);//175 238 238
        else
            tld.setBackground(new Color(220,220,220));//175 238 238
        label.setForeground(Color.BLACK);
        label.setLayoutData(tld);
        label.setFont(new Font(Font.ARIAL, 1, new Extent(11)));
        return label;
    }

}